package com.woniu.user.controller;


import com.woniu.entity.Result;
import com.woniu.entity.ResultCode;
import com.woniu.user.DTO.CommentDTO;
import com.woniu.user.DTO.UserLoginDTO;
import com.woniu.user.DTO.UserRegisterDTO;
import com.woniu.user.pojo.User;
import com.woniu.user.service.IUserService;
import com.woniu.user.util.SMSUtils;
import com.woniu.util.JwtUtil;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import com.woniu.entity.Result;
import com.woniu.order.pojo.Order;
import com.woniu.user.pojo.User;
import com.woniu.user.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import com.woniu.entity.Result;
import com.woniu.user.pojo.User;
import com.woniu.user.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.List;

/**
 * <p>
 * 用户端用户表 前端控制器
 * </p>
 *
 * @author LiFangyu
 * @since 2023-02-15
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户管理")
public class UserController {

    @Autowired
    private IUserService userService;

    @Autowired
    private RedisTemplate redisTemplate;


    @ApiOperation("登录")
    @PostMapping("/login")
    public Result<Map<String, Object>> login(@Valid@RequestBody UserLoginDTO userLoginDTO){
        User user = userService.login(userLoginDTO);
        if(user == null){
            // 登录失败，用户名或者密码错误
            return Result.error(ResultCode.USER_LOGIN_ERROR.getCode(), ResultCode.USER_LOGIN_ERROR.getMessage());
        }
        // 登录成功，生成token，并且将token存放到Redis中
        Map<String, Object> claims = new HashMap<>();
        claims.put("user", user);
        String token  = JwtUtil.createJWT(user.getId(), claims, 60 * 60 * 60L);
        // 将token存储到Redis中
        redisTemplate.opsForValue().set("token_" + token, token, 60 * 30L, TimeUnit.SECONDS);
        // 登录成功，构建返回的数据
        Map<String, Object> map = new HashMap<>();
        map.put("Authorization", token);
        return Result.ok(map);
    }

    @ApiOperation("注册")
    @PostMapping("/register")
    public Result register(@Valid @RequestBody UserRegisterDTO userRegisterDTO){
        if (!userRegisterDTO.getAgreement()){
            return Result.error(401,"请先同意协议合同");
        }
        Result user = userService.register(userRegisterDTO);
        return user;
    }

    @ApiOperation("获取验证码")
    @GetMapping("/getVerificationCode/{phone}")
    public Result  getVerificationCode(@PathVariable String phone){
       return userService.getVerificationCode(phone);
    }


    @ApiOperation("上传头像")
    @PostMapping("/addPortrait/{phone}")
    public Result addPortrait(@RequestPart MultipartFile multipartFile,@PathVariable String phone){
        return userService.addPortrait(multipartFile,phone);
    }

    @ApiOperation("/我的信息：关注/粉丝/获赞/收藏 数目")
    @GetMapping("/personalInformation/{phone}")
    public Result personalInformation(@PathVariable String phone){
        return userService.personalInformation(phone);
    }

    @ApiOperation("获取关注列表")
    @GetMapping("/getAttention/{phone}")
    public Result getAttention(@PathVariable String phone){
        return userService.getAttention(phone);
    }

    @ApiOperation("获取粉丝列表")
    @GetMapping("/getFans/{phone}")
    public Result getFans(@PathVariable String phone){
        return userService.getFans(phone);
    }

    @ApiOperation("获取收藏列表")
    @GetMapping("/getCollect/{phone}")
    public Result getCollect(@PathVariable String phone){
        return userService.getCollect(phone);
    }


    @GetMapping("/get/{username}")
    @ApiOperation("根据用户名查询用户信息")
    public Result<User> getByUsername(@PathVariable String username){
       User user = userService.getByUsername(username);
       return Result.ok(user);
    }

    @PostMapping("/select/{username}")//phone
    @ApiOperation("根据token查询订单信息")
    public Result<List<Order>> getOrderInfo(@PathVariable String username){
        //解析token
//        String token = request.getHeader("Authorization");
//        Claims claims = JwtUtil.parseJWT(token);
//        String username = (String) claims.get("username");
        List<Order> userOrderInfo = userService.getOrderInfo(username);
        return Result.ok(userOrderInfo);
    }

    @GetMapping("/getUserCircle/{phone}")
    @ApiOperation("获取用户关注的车圈")
    public Result getUserCircle(@PathVariable String phone){
       return userService.getUserCircle(phone);
    }

    @GetMapping("/getUserLikeCar/{phone}")
    @ApiOperation("获取用户关注的车辆")
    public Result getUserLikeCar(@PathVariable String phone){
        return userService.getUserLikeCar(phone);
    }

    @ApiOperation("用户评论")
    @PostMapping("/comment")
    public Result comment(CommentDTO commentDTO){
        return userService.comment(commentDTO);
    }

    @GetMapping("/getUserCar")
    @ApiOperation("获取用户的车辆")
    public Result getUserCar(@PathVariable String phone){
        return userService.getUserCar(phone);
    }

    @GetMapping("/getUserByPhone/{phone}")
    @ApiOperation("根据用户手机号获取用户")
    public User getUserByPhone(@PathVariable("phone") String phone){
        User user = userService.getUserByPhone(phone);
        System.out.println(user);
        return user;
    }

    @GetMapping("/shopGetUserById/{id}")
    @ApiOperation("根据用户ID查询用户手机号")
    public Result<String> shopGetUserById(@PathVariable String id){
        User user = userService.getById(id);
        if (user != null){
            return Result.ok(user.getPhone());
        }
        return Result.error(ResultCode.RESULE_DATA_NONE.getCode(),ResultCode.RESULE_DATA_NONE.getMessage());
    }

}
